/*******************************************************************************
 * Copyright (c) 2006 Ultimate Technology, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bojan Vukojevic - initial API and implementation
 *******************************************************************************/
package com.ultimatetech.cim.views.content;

import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.client.CIMClient;
import javax.wbem.client.PasswordCredential;
import javax.wbem.client.UserPrincipal;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.ui.PlatformUI;

import com.ultimatetech.cim.CIMPlugin;
import com.ultimatetech.cim.preferences.CIMPreferencePage;

public class CIMConnect
{
    public static CIMClient connect(String prot, String host, String port,
            String names)
    {
        CIMClient client = null;
        // System.out.println("connect to:" + ns);
        String username = CIMPlugin.getDefault().getCIMUsername();
        String pwd = CIMPlugin.getDefault().getCIMPassword();
        UserPrincipal up = new UserPrincipal(username);
        PasswordCredential pc = new PasswordCredential(pwd);
        prot = prot == null || "".equals(prot) ? "http" : prot;
        host = host == null || "".equals(host) ? "localhost" : host;
        port = port == null || "".equals(port) ? "5988" : port;
        CIMNameSpace ns = null;
        try
        {
            if (prot.equals(CIMClient.CIM_RMI))
            {
                ns = new CIMNameSpace(host, names);
                client = new CIMClient(ns, up, pc, CIMClient.CIM_RMI);
            }
            else
            {
                ns = new CIMNameSpace(prot + "://" + host + ":" + port, names);
                client = new CIMClient(ns, up, pc);
            }
        }
        catch (CIMException e)
        {
            ErrorDialog ed = new ErrorDialog(PlatformUI.getWorkbench()
                    .getActiveWorkbenchWindow().getShell(), "Connection Error",
                    "Connection failed:" + e.getMessage(), new Status(
                            Status.ERROR, "CIMPlugin", e.getXmlCode(e.getID()),
                            e.getMessage(), e), IStatus.ERROR);
            ed.open();
        }
        return client;
    }

    public static CIMClient connectUsingPreferences()
    {
        Preferences pref = CIMPlugin.getDefault().getPluginPreferences();
        return connect(
                pref.getString(CIMPreferencePage.PreferenceNames.IS_SECURE),
                pref.getString(CIMPreferencePage.PreferenceNames.CIM_HOST),
                pref.getString(CIMPreferencePage.PreferenceNames.CIM_PORT),
                pref.getString(CIMPreferencePage.PreferenceNames.CIM_NAMESPACE));
    }
}
